<?php

class Model_Api_V2_Guide
{
    const TYPE_ID = 106;

    /**
     * 获取价格筛选条件
     * @return array
     */
    public static function get_price_condition()
    {
        $data = DB::select()->from('guide_pricelist')
            ->where('lowerprice', '>=', 0)
            ->order_by('lowerprice', 'asc')
            ->execute()
            ->as_array();
        foreach ($data as &$row) {
            if ($row['lowerprice'] == 0 && $row['highprice'] != 0) {
                $row['name'] = Currency_Tool::symbol() . $row['highprice'] . '以下';
            } else if ($row['highprice'] == '' || $row['highprice'] == 0) {
                $row['name'] = Currency_Tool::symbol() . $row['lowerprice'] . '以上';
            } else if ($row['lowerprice'] != '' && $row['highprice'] != '') {
                $row['name'] = Currency_Tool::symbol() . $row['lowerprice'] . '-' . Currency_Tool::symbol() . $row['highprice'];
            }
        }

        return $data;
    }


    /**
     * 获取属性筛选条件
     * @return array
     */
    public static function get_attribute_condition()
    {
        $data      = Model_Api_V2_Guide::get_attribute_by_pid();
        $attribute = array();
        foreach ($data as &$item) {
            $child = Model_Api_V2_Guide::get_attribute_by_pid($item['id']);
            if ($child) {
                $item['child'] = $child;
                $attribute[]   = $item;
            }
        }
        return $attribute;
    }

    /**
     * 通过父级id获取属性
     * @param $pid
     * @return array
     */
    public static function get_attribute_by_pid($pid = 0)
    {
        return DB::select('id', array('attrname', 'name'))
            ->from('guide_attr')
            ->where('pid', '=', $pid)
            ->and_where('isopen', '=', 1)
            ->order_by(DB::expr('ifnull(displayorder,9999)'), 'asc')
            ->execute()
            ->as_array();
    }

    /**
     * 通过mid获取导游信息
     * @param $mid
     * @return array
     */
    public static function get_guide_by_mid($mid)
    {
        return DB::select()
            ->from('guide')
            ->where('mid', '=', $mid)
            ->execute()
            ->current();
    }

    /**
     * 检查导游是否有效，有效返回导游信息
     * @param $mid
     * @param $mid
     * @return array
     * @throws Exception
     */
    public static function check_guide_is_available($mid)
    {
        $guide = Model_Api_V2_Guide::get_guide_by_mid($mid);
        if (empty($guide) || $guide['status'] != 1) {
            throw new Exception('未开通导游/认证未通过');
        }

        return $guide;
    }

    /**
     * @function 导游服务详情
     * @param $id
     * @return mixed
     */
    public static function get_info($id)
    {
        $service = DB::select()
            ->from('guide_service')
            ->where('id', '=', $id)
            ->and_where('webid', '=', $GLOBALS['sys_webid'])
            ->execute()
            ->current();
        if (!$service) {
            return false;
        }

        $service['typeid']            = self::TYPE_ID;
        $service['sellnum']           = Model_Member_Order::get_sell_num($service['id'], 106) + intval($service['bookcount']);
        $service['commentnum']        = Model_Comment::get_comment_num($service['id'], 106); //评论次数
        $service['attrid']            = Model_Guide_Attr::get_attrname_list($service['attrid']);
        $service['jifentprice_info']  = Model_Jifen_Price::get_used_jifentprice($service['jifentprice_id'], self::TYPE_ID);
        $service['jifenbook_info']    = Model_Jifen::get_used_jifenbook($service['jifenbook_id'], self::TYPE_ID);
        $service['jifencomment_info'] = Model_Jifen::get_used_jifencomment(self::TYPE_ID);
        $service['iconlist']          = Product::get_ico_list($service['iconlist']);
        $service['price']             = Currency_Tool::price($service['price']);
        $service['satisfyscore']      = Model_Comment::get_score($service['id'], self::TYPE_ID, $service['satisfyscore'], $service['commentnum']);//满意度
        $service['score']             = St_Functions::get_satisfy(106, $service['id'], $service['satisfyscore']);
        //详情内容
        $params                 = array(
            'typeid' => '106',
            'productinfo' => $service,
            'onlyrealfield' => 1,
            'pc' => 0

        );
        $service['detail_list'] = Model_Guide_Service::get_detail_content($params);

        return $service;
    }
}
